package com.springseries.poi.user.export;

import com.springseries.poi.user.model.UserModel;
import com.springseries.poi.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.servlet.view.document.AbstractXlsxStreamingView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * excel导出规则
 */
public class UserExcelBuilder extends AbstractXlsxStreamingView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        String filename = "用户信息.xlsx";// 设置下载时客户端Excel的名称
        filename = ExcelUtil.encodeFilename(filename, request);// 处理中文文件名
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        Sheet sheet = workbook.createSheet("用户信息");
        List<UserModel> userList = (List<UserModel>) model.get("userList");
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
        Row row = sheet.createRow(0);
        Cell cell0 = row.createCell(0);// 第一列
        Cell cell1 = row.createCell(1);
        Cell cell2 = row.createCell(2);
        Cell cell3 = row.createCell(3);

        cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell3.setCellType(HSSFCell.CELL_TYPE_STRING);

        cell0.setCellValue("序号");
        cell1.setCellValue("名称");
        cell2.setCellValue("数量");
        cell3.setCellValue("版本");

        for (int i = 0; i < userList.size(); i++) {
            UserModel user = userList.get(i);
            Row rowData = sheet.createRow(i + 1);

            Cell cellData0 = rowData.createCell(0);
            cellData0.setCellStyle(cellStyle);
            cellData0.setCellType(HSSFCell.CELL_TYPE_STRING);
            cellData0.setCellValue(i + 1);

            Cell cellData1 = rowData.createCell(1);
            cellData1.setCellStyle(cellStyle);
            cellData1.setCellType(HSSFCell.CELL_TYPE_STRING);
            cellData1.setCellValue(user.getName() != null ? user.getName() : "");

            Cell cellData2 = rowData.createCell(2);
            cellData2.setCellStyle(cellStyle);
            cellData2.setCellType(HSSFCell.CELL_TYPE_STRING);
            cellData2.setCellValue(user.getQuantity());

            Cell cellData3 = rowData.createCell(3);
            cellData3.setCellStyle(cellStyle);
            cellData3.setCellType(HSSFCell.CELL_TYPE_STRING);
            cellData3.setCellValue(user.getVersion());
        }
    }
}
